package org.ling.xuan.rule;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.ling.xuan.config.SysConstant;
import org.ling.xuan.config.SystemProperties;
import org.ling.xuan.context.AddPipeLineContext;
import org.ling.xuan.context.CheckChannelContext;
import org.ling.xuan.dao.LocalAddDao;
import org.ling.xuan.entity.mo.LocalAdd;
import org.ling.xuan.util.LxOptional;
import org.ling.xuan.util.LxStrUtil;

public class FilePathNameCheckRule extends CheckRule<CheckChannelContext, AddPipeLineContext> {
    @Override
    public void check(AddPipeLineContext pipeContext, CheckChannelContext channelContext) {
        String bankCode = pipeContext.getLocalAdd().getBankCode();
        String[] bankCodes = bankCode.split(";");
        LocalAdd localAdd = LocalAddDao.getCur();
        String filePath = pipeContext.getFilePath();
        String name = FileUtil.getName(pipeContext.getFilePath());
        if(FileUtil.getPrefix(filePath).contains(" ")){
            pipeContext.error("文件名不能包含空格");
        }
        if(SystemProperties.isTrueDefaultTrue(SysConstant.checkBankCodePathMatch)) {
            if (StrUtil.isBlank(LxOptional.of(pipeContext, p -> p.getLocalAdd().getBankCode()).orElse(""))) {
                pipeContext.warn("添加信息客户号未填,跳过文件路径bank下文件检查");
            } else {
                String parent = FileUtil.getParent(pipeContext.getFilePath(),0);
                if ((name.endsWith(".sql")||name.endsWith(".upd"))&&localAdd.getDeployType().equals("sp")) {
                    if(!parent.contains(localAdd.getModifyVersion())){
                        pipeContext.error("sp包提交路径未包含版本号,请检查是否放错文件夹");
                    }
                }
                if ((name.endsWith(".sql")||name.endsWith(".upd"))&&localAdd.getDeployType().equals("req")) {
                    boolean ct = false;
                    for(String s:bankCodes){
                        if(parent.contains(s)){
                            ct = true;
                            break;
                        }
                    }
                    if(!ct){
                        pipeContext.error("小包提交不包含客户代码,请检查是否放错文件夹");
                    }
                }
            }
        }
        if (StrUtil.isBlank(localAdd.getRequirementId())) {
            pipeContext.warn("未填写需求id,无法检查sql和upd文件是否以需求id命名");
        } else {
            if(SystemProperties.isTrueDefaultTrue(SysConstant.checkRequestIdAsSqlName)) {
                if ((name.endsWith(".sql")||name.endsWith("upd"))&&localAdd.getDeployType().equals("req")&&!name.contains(localAdd.getRequirementId())) {
                    pipeContext.error("存在修改sql的情况下,修改的sql,upd必须以需求编号命名");
                }
            }
            if(SystemProperties.isTrueDefaultTrue(SysConstant.checkVersionIdAsSqlName)){
                if ((name.endsWith(".sql")||name.endsWith("upd"))&&localAdd.getDeployType().equals("sp")) {
                    if(!name.contains(localAdd.getModifyVersion())) {
                        pipeContext.error("sp包提交文件名需要包含版本号");
                    }
                }
            }
        }
    }

    @Override
    public boolean match(AddPipeLineContext pipeContext, CheckChannelContext channelContext) {
        return FileUtil.isFile(pipeContext.getFilePath());
    }

}
